'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 角色相关
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var taskService = require('./taskService');
var analyze = require('../libs/analyze');
var roleModel = require('../models/roleModel');
var battleModel = require('../models/battleModel');
var unionGroupModel = require('../models/unionGroupModel');
var buildType = require('../consts/buildType');
var buildConf = require('../config/buildConf');
var heroConstellationConf = require('../config/heroConstellationConf.js');
var heroAttr = require('../libs/war/heroAttr');
var activityService = require('../services/activityService');
var actType = require('../consts/activityType.js');
var fightPowerService = require('./fightPowerService.js');
var constantUtil = require('../util/constantUtil.js');
var techType = require('../consts/techType.js');
var moduleType = require('../consts/moduleType.js');
var operationType = require('../consts/operationType.js');
var accruedType = require('../consts/accruedType.js');
var blackMarketConf = require('../config/blackMarketConf.js');
var vipConf = require('../config/vipConf.js');

/**
 * 添加金币,这个接口会判断最大值,如果是直接购买金币的,不需要调用该接口
 * 调用这个接口必须要加载 tech模块
 * @param {[type]} add_gold [description]
 */
exports.addGoldLimit = function (player_manager, module_type, operation_type, add_gold, reserve_int_val, reserve_str_val) {
    add_gold = parseInt(add_gold);
    var build_model = player_manager.build;
    var role_model = player_manager.role;
    var max_gold_store = build_model.getMaxGoldStore();

    if (role_model.getGold() + add_gold > max_gold_store) {
        add_gold = max_gold_store - role_model.getGold();
        if (add_gold < 0) {
            add_gold = 0;
        }
    }
    role_model.addGold(add_gold);
    taskService.addTotalGold(player_manager, add_gold);
    var role_id = player_manager.getRoleId();
    var channel_id = role_model.channel_id;
    analyze.recordGoldGet(role_id, channel_id, module_type, operation_type, add_gold, reserve_int_val, reserve_str_val);
    return add_gold;
};

/**
 * 添加金币,无不限限制
 * @param {[type]} role_model [description]
 * @param {[type]} add_gold   [description]
 */
exports.addGold = function (player_manager, module_type, operation_type, add_gold, reserve_int_val, reserve_str_val) {
    add_gold = parseInt(add_gold);
    if (add_gold < 0) {
        add_gold = 0;
    }
    taskService.addTotalGold(player_manager, add_gold);

    var role_model = player_manager.role;
    role_model.addGold(add_gold);

    var role_id = player_manager.getRoleId();
    var channel_id = role_model.channel_id;
    analyze.recordGoldGet(role_id, channel_id, module_type, operation_type, add_gold, reserve_int_val, reserve_str_val);
};

/**
 * 扣除金币
 * @param  {[type]} player_manager [description]
 * @param  {[type]} sub_gold       [description]
 * @return {[type]}                [description]
 */
exports.subGold = function (player_manager, module_type, operation_type, sub_gold, reserve_int_val, reserve_str_val) {
    sub_gold = parseInt(sub_gold);
    var role_model = player_manager.role;
    var is_success = role_model.subGold(sub_gold);

    // 增加7天消耗统计
    var accrued_model = player_manager.accrued;
    accrued_model.addStaticList(accruedType.GOLD, sub_gold);

    if (!!is_success) {
        var role_id = player_manager.getRoleId();
        var channel_id = role_model.channel_id;
        analyze.recordGoldUse(role_id, channel_id, module_type, operation_type, sub_gold, reserve_int_val, reserve_str_val);
    }
    return is_success;
};

/**
 * 添加食物,这个接口会判断最大值,如果是直接购买金币的,不需要调用该接口
 * @param {[type]} add_food [description]
 */
exports.addFoodLimit = function (player_manager, module_type, operation_type, add_food, reserve_int_val, reserve_str_val) {
    add_food = parseInt(add_food);
    var role_model = player_manager.role;
    var build_model = player_manager.build;
    var max_food_store = build_model.getMaxFoodStore();

    if (role_model.getFood() + add_food > max_food_store) {
        add_food = max_food_store - role_model.getFood();
        if (add_food < 0) {
            add_food = 0;
        }
    }

    role_model.addFood(add_food);

    var role_id = player_manager.getRoleId();
    var channel_id = role_model.channel_id;
    analyze.recordFoodGet(role_id, channel_id, module_type, operation_type, add_food, reserve_int_val, reserve_str_val);
    return add_food;
};

/**
 * 添加粮食,无上限
 * @param {[type]} role_model [description]
 * @param {[type]} add_gold   [description]
 */
exports.addFood = function (player_manager, module_type, operation_type, add_food, reserve_int_val, reserve_str_val) {
    add_food = parseInt(add_food);
    var role_model = player_manager.role;
    role_model.addFood(add_food);

    var role_id = player_manager.getRoleId();
    var channel_id = role_model.channel_id;
    analyze.recordFoodGet(role_id, channel_id, module_type, operation_type, add_food, reserve_int_val, reserve_str_val);
};

/**
 * 扣除粮食
 * @param  {[type]} player_manager [description]
 * @param  {[type]} sub_food       [description]
 * @return {[type]}                [description]
 */
exports.subFood = function (player_manager, module_type, operation_type, sub_food, reserve_int_val, reserve_str_val) {
    sub_food = parseInt(sub_food);
    var role_model = player_manager.role;
    var is_success = role_model.subFood(sub_food);

    // 增加7天消耗统计
    var accrued_model = player_manager.accrued;
    accrued_model.addStaticList(accruedType.FOOD, sub_food);

    if (!!is_success) {
        var role_id = player_manager.getRoleId();
        var channel_id = role_model.channel_id;
        analyze.recordFoodUse(role_id, channel_id, module_type, operation_type, sub_food, reserve_int_val, reserve_str_val);
    }
    return is_success;
};

/**
 * VIP等级变化
 * @param {[type]} add_diamond [description]
 */
/* jshint maxstatements:100 */
exports.vipChanged = function (player_manager, old_vip_lv, new_vip_lv) {
    var role_model = player_manager.role;
    var wishtree_model = player_manager.wishtree;
    var market_model = player_manager.market;
    var copy_model = player_manager.copy;
    var food_carbon_model = player_manager.food_carbon;
    var gold_carbon_model = player_manager.gold_carbon;
    var opt_ok = true;
    var vip_conf = vipConf.getConf(new_vip_lv);
    var old_vip_conf = vipConf.getConf(old_vip_lv);
    if (!vip_conf) {
        console.error('invalid vip lv:', new_vip_lv);
        return false;
    }

    if (!old_vip_conf) {
        console.error('invalid vip lv:', old_vip_conf);
        return false;
    }
    // 活动粮食副本vip次数
    if (!!food_carbon_model) {
        var vip_food_carbon_num = vip_conf.add_food_carbon_challenge_times;
        var diff_vip_food_carbon_num = vip_food_carbon_num - old_vip_conf.add_food_carbon_challenge_times;
        food_carbon_model.addFightTimes(diff_vip_food_carbon_num);
        food_carbon_model.setFightTimes(vip_food_carbon_num);
    } else {
        console.warn('roleService.vipChanged(), food_carbon_model = null');
        opt_ok = false;
    }

    // 活动金币副本vip次数
    if (!!gold_carbon_model) {
        var vip_gold_carbon_num = vip_conf.add_gold_carbon_challenge_times;
        var diff_vip_gold_carbon_num = vip_gold_carbon_num - old_vip_conf.add_gold_carbon_challenge_times;
        gold_carbon_model.addFightTimes(diff_vip_gold_carbon_num);
        gold_carbon_model.setFightTimes(vip_gold_carbon_num);
    } else {
        console.warn('roleService.vipChanged(), food_carbon_model = null');
        opt_ok = false;
    }

    // 掠夺免费次数
    if (!!role_model) {
        var vip_pvp_num = vip_conf.add_pvp_reward_times;
        role_model.addHarryCountTimes(vip_pvp_num);
    } else {
        console.warn('roleService.vipChanged(), role_model = null');
        opt_ok = false;
    }

    // 许愿树免费次数
    if (!!wishtree_model) {
        var vip_wishtree_num = vip_conf.add_wish_times;
        var diff_vip_wishtree_num = vip_wishtree_num - old_vip_conf.add_wish_times;
        wishtree_model.addFreeNum(diff_vip_wishtree_num);
        wishtree_model.setVipNum(vip_wishtree_num);
    } else {
        console.warn('roleService.vipChanged(), wishtree_model = null');
        opt_ok = false;
    }

    // 市场免费次数
    if (!!market_model) {
        var old_vip_market_num = 0;
        var add_vip_market_num = 0;
        var vip_lv;

        for (vip_lv = 1; vip_lv <= old_vip_lv; vip_lv++) {
            old_vip_market_num += vipConf.getConf(vip_lv).add_market_fresh_times;
        }
        for (vip_lv = old_vip_lv + 1; vip_lv <= new_vip_lv; vip_lv++) {
            add_vip_market_num += vipConf.getConf(vip_lv).add_market_fresh_times;
        }
        market_model.addFreeNum(add_vip_market_num);
        market_model.setVipNum(old_vip_market_num + add_vip_market_num);
    } else {
        console.warn('roleService.vipChanged(), market_model = null');
        opt_ok = false;
    }

    // 增加精英副本次数
    if (!!copy_model) {
        var vip_hard_carbon_times = vip_conf.hard_carbon_times;
        copy_model.addVipAddHardCarbonTimes(vip_hard_carbon_times);
    } else {
        console.warn('roleService.vipChanged(), copy_model = null');
        opt_ok = false;
    }

    return opt_ok;
};

/**
 * 添加钻石
 * @param {[type]} add_diamond [description]
 */
exports.addDiamond = function (player_manager, module_type, operation_type, add_diamond, reserve_int_val, reserve_str_val) {
    add_diamond = parseInt(add_diamond);
    var role_model = player_manager.role;
    role_model.addDiamond(add_diamond);
    taskService.addTotalDiamond(player_manager, add_diamond);

    var role_id = player_manager.getRoleId();
    var channel_id = role_model.channel_id;
    analyze.recordDiamondGet(role_id, channel_id, module_type, operation_type, add_diamond, reserve_int_val, reserve_str_val);
};

/**
 * 扣除钻石
 * @param  {[type]} player_manager [description]
 * @param  {[type]} sub_diamond    [description]
 * @return {[type]}                [description]
 */
exports.subDiamond = function (player_manager, module_type, operation_type, sub_diamond, reserve_int_val, reserve_str_val) {
    sub_diamond = parseInt(sub_diamond);
    if (sub_diamond < 1) {
        console.warn('roleService->subDiamond(), invalid diamond cnt:', sub_diamond);
        return true;
    }
    var role_model = player_manager.role;

    // 增加7天消耗统计
    var accrued_model = player_manager.accrued;
    accrued_model.addStaticList(accruedType.DIAMOND, sub_diamond);

    var success = role_model.subDiamond(sub_diamond);
    if (!!success) {
        var role_id = player_manager.getRoleId();
        var channel_id = role_model.channel_id;
        analyze.recordDiamondUse(role_id, channel_id, module_type, operation_type, sub_diamond, reserve_int_val, reserve_str_val);
    }
    return success;
};

/**
 * 添加充值钻石
 * @param {[type]} add_diamond [description]
 */
exports.addPayDiamond = function (player_manager, add_diamond, reserve_int_val) {
    add_diamond = parseInt(add_diamond);

    var role_model = player_manager.role;
    role_model.addPayDiamond(add_diamond);
    taskService.addTotalDiamond(player_manager, add_diamond);

    var role_id = player_manager.getRoleId();
    var channel_id = role_model.channel_id;
    analyze.recordDiamondGet(role_id, channel_id, moduleType.MDL_PAY, operationType.OPR_PAY, add_diamond, reserve_int_val);
};

/**
 * 添加兵魂
 * @param {[type]} add_soul [description]
 */
exports.addSoldierSoul = function (player_manager, module_type, operation_type, add_soul, reserve_int_val, reserve_str_val) {
    add_soul = parseInt(add_soul);
    if (add_soul < 1) {
        console.warn('roleService->addSoldierSoul(), invalid add value of soul:', add_soul);
        return;
    }
    var role_model = player_manager.role;
    role_model.addSoldierSoul(add_soul);

    var role_id = player_manager.getRoleId();
    var channel_id = role_model.channel_id;
    analyze.recordSoldierSoulGet(role_id, channel_id, module_type, operation_type, add_soul, reserve_int_val, reserve_str_val);
};

/**
 * 扣除兵魂
 * @param  {[type]} player_manager [description]
 * @return {[type]}                [description]
 */
exports.subSoldierSoul = function (player_manager, module_type, operation_type, sub_soul, reserve_int_val, reserve_str_val) {
    sub_soul = parseInt(sub_soul);
    if (sub_soul < 1) {
        console.warn('roleService->subSoldierSoul(), invalid sub value of soul:', sub_soul);
        return false;
    }
    var role_model = player_manager.role;

    // 增加7天消耗统计
    var accrued_model = player_manager.accrued;
    accrued_model.addStaticList(accruedType.SOLDIER_SOUL, sub_soul);

    var success = role_model.subSoldierSoul(sub_soul);
    if (!!success) {
        var role_id = player_manager.getRoleId();
        var channel_id = role_model.channel_id;
        analyze.recordSoldierSoulUse(role_id, channel_id, module_type, operation_type, sub_soul, reserve_int_val, reserve_str_val);
    }

    return success;
};

/**
 * 添加龙币
 * @param {[type]} add_diamond [description]
 */
exports.addDragonGold = function (player_manager, module_type, operation_type, add_dragon_gold, reserve_int_val, reserve_str_val) {
    add_dragon_gold = parseInt(add_dragon_gold);
    if (add_dragon_gold < 1) {
        console.warn('roleService->addDragonGold(), invalid add value of dragon_gold:', add_dragon_gold);
        return;
    }
    var role_model = player_manager.role;
    role_model.addDragonGold(add_dragon_gold);

    var role_id = player_manager.getRoleId();
    var channel_id = role_model.channel_id;
    analyze.recordDragonGoldGet(role_id, channel_id, module_type, operation_type, add_dragon_gold, reserve_int_val, reserve_str_val);
};

/**
 * 扣除龙币
 * @param  {[type]} player_manager [description]
 * @param  {[type]} sub_diamond    [description]
 * @return {[type]}                [description]
 */
exports.subDragonGold = function (player_manager, module_type, operation_type, sub_dragon_gold, reserve_int_val, reserve_str_val) {
    sub_dragon_gold = parseInt(sub_dragon_gold);
    if (sub_dragon_gold < 1) {
        console.warn('roleService->subDragonGold(), invalid sub value of dragon_gold:', sub_dragon_gold);
        return;
    }
    var role_model = player_manager.role;
    var success = role_model.subDragonGold(sub_dragon_gold);
    if (!!success) {
        var role_id = player_manager.getRoleId();
        var channel_id = role_model.channel_id;
        analyze.recordDragonGoldUse(role_id, channel_id, module_type, operation_type, sub_dragon_gold, reserve_int_val, reserve_str_val);
    }

    return success;
};

/**
 * 添加经验
 * @param {[type]} player_manager [description]
 * @param {[type]} add_exp        [description]
 */
exports.addExp = function (player_manager, add_exp) {
    var self = this;
    add_exp = parseInt(add_exp);
    var nexus_build = player_manager.build.getBuildByType(buildType.NEXUS);
    var nexus_lv_conf = buildConf.getBuildLvConf(buildType.NEXUS, nexus_build.getLv());
    var hero_lvl_limit = nexus_lv_conf.hero_lvl_limit;
    if (hero_lvl_limit < player_manager.role.getLv()) {
        return;
    }
    var old_lvl = player_manager.role.getLv();
    player_manager.role.addExp(add_exp, hero_lvl_limit);
    var new_lvl = player_manager.role.getLv();
    if (old_lvl !== new_lvl) {
        this.updateHeroPower(player_manager);
        activityService.push(player_manager.getRoleId(), actType.GROW, player_manager);
        activityService.push(player_manager.getRoleId(), actType.HERO_LV, player_manager);

        var lvl_up_recover_sp = constantUtil.value('lvl_up_recover_sp');
        var add_energy = lvl_up_recover_sp * (new_lvl - old_lvl);
        self.addEnergy(player_manager, moduleType.MDL_OTHER, operationType.OPR_OTHER_LVL_UP_ADD_ENERGY, add_energy);

        var role_id = player_manager.getRoleId();
        analyze.recordRoleLevel(role_id, new_lvl);
    }
    taskService.addHeroLv(player_manager, player_manager.role.getLv());
};

/**
 * 添加保护时间
 * @param {[type]} player_manager   [description]
 * @param {[type]} add_protect_time [description]
 */
exports.addProtectTime = function (player_manager, add_protect_time) {
    add_protect_time = parseInt(add_protect_time);
    player_manager.role.addProtectTime(add_protect_time);
    taskService.addProtectTime(player_manager, add_protect_time);
};

/**
 * 增加繁荣度
 * @param {[type]} add_magic [description]
 */
exports.addProsperous = function (player_manager, add_val) {
    add_val = parseInt(add_val);
    return player_manager.role.addProsperous(add_val);
};

/**
 * 减少繁荣度
 * @param  {[type]} player_manager [description]
 * @return {[type]}                [description]
 */
exports.subProsperous = function (player_manager, sub_val) {
    sub_val = parseInt(sub_val);
    return player_manager.role.subProsperous(sub_val);
};

/**
 * 增加军功
 * @param {[type]} add_magic [description]
 */
exports.addHonor = function (player_manager, add_val) {
    add_val = parseInt(add_val);
    return player_manager.role.addHonor(add_val);
};

/**
 * 减少军功
 * @param  {[type]} player_manager [description]
 * @return {[type]}                [description]
 */
exports.subHonor = function (player_manager, sub_val) {
    sub_val = parseInt(sub_val);
    return player_manager.role.subHonor(sub_val);
};

/**
 * 获取玩家基本信息
 */
exports.getRoleInfo = function (role_id, cb) {
    var role_info = {};
    async.waterfall(
        [

            function (cb) {
                roleModel.getByRoleId(role_id, cb);
            },
            function (role_model, cb) {
                if (!!role_model) {
                    role_info.name = role_model.name;
                    role_info.lv = role_model.lv;
                    role_info.job_type = role_model.job_type;
                    role_info.role_id = role_id;
                    role_info.vip = role_model.vip;
                    role_info.union_id = role_model.union_id;
                    role_info.union_name = role_model.union_name;
                    role_info.union_flag = role_model.union_flag;
                }
                cb();
            },
            function (cb) {
                battleModel.getByRoleId(role_id, cb);
            },
            function (battle_model, cb) {
                role_info.cup = battle_model.getCup();
                role_info.noble = battle_model.noble;
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return cb();
            }
            cb(null, role_info);
        }
    );
};

/**
 * 获取玩家信息列表
 */
exports.getRoleList = function (role_id_list, callback) {
    var where = {
        role_id: {
            $in: role_id_list
        }
    };

    var battle_map = {};
    var role_list = [];
    async.waterfall(
        [

            function (cb) {
                battleModel.findWhere(where, cb);
            },
            function (list, cb) {
                _.forEach(list, function (item) {
                    battle_map[item.role_id] = item;
                });
                roleModel.findRoles(where, cb);
            },
            function (list, cb) {
                _.forEach(list, function (role) {
                    var role_info = {
                        role_id: role.role_id,
                        name: role.name,
                        lv: role.lv,
                        job_type: role.job_type,
                        vip: role.vip,
                        union_id: role.union_id,
                        union_name: role.union_name,
                        union_flag: role.union_flag
                    };

                    var battle_info = battle_map[role.role_id];
                    if (!!battle_info) {
                        role_info.cup = battle_info.cup;
                        role_info.noble = battle_info.noble;
                    }
                    role_list.push(role_info);
                });

                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return callback(null);
            }
            callback(null, role_list);
        }
    );
};

/**
 * 更新英雄战力
 * @param  {[type]}   role_model      [角色]
 * @param  {[type]}   bag_model       [背包]
 * @param  {[type]}   skill_model     [技能]
 * @param  {[type]}   build_model     [建筑]
 * @return {[type]}                   [战力值]
 */
exports.updateHeroPower = function (player_manager, update_single) {
    var self = this;
    var need_list = ['role', 'bag', 'skill', 'build'];
    var real_load = [];

    _.each(need_list, function (model_name) {
        if (!player_manager[model_name]) {
            real_load.push(model_name);
        }
    });

    if (real_load.length > 0) {
        // 如果没有装入所需的数据表，则装入数据表之后，再异步刷新英雄战力
        player_manager.getModel(real_load, function (err) {
            if (!!err) {
                console.error(err, '计算英雄战力出错（装入相关模块数据出错）');
                return;
            }
            self.updateHeroPower(player_manager);
        });

        return;
    }

    var role_model = player_manager.role;
    var bag_model = player_manager.bag;
    var skill_model = player_manager.skill;
    var build_model = player_manager.build;

    var attr = heroAttr.getAttr(role_model, bag_model, skill_model, build_model).attr;
    var att_val = (attr.min_atk || 0) +
        (attr.fire_damage || 0) +
        (attr.ice_damage || 0) +
        (attr.light_damage || 0) +
        (attr.city_damage || 0);
    var hp_val = attr.hp;

    var constellation_fp = 0;
    var constellation_lv = role_model.constellation_lv;
    if (constellation_lv > 0) {
        var constellation_conf = heroConstellationConf.getConf(constellation_lv);
        _.forEach(constellation_conf, function (item) {
            constellation_fp += item.fp;
        });
    }
    role_model.hero_power = Math.floor(Math.pow((att_val * 3 / 2) * hp_val, 0.5) + constellation_fp);

    if (!update_single) {
        // 刷新总战力
        fightPowerService.updByIdMainPower(player_manager.getRoleId(), player_manager, function (err) {
            if (!err) {
                player_manager.save();
            }
        });
    }
};

/**
 * 添加体力,无上限
 */
exports.addEnergy = function (player_manager, module_type, operation_type, add_energy, reserve_int_val, reserve_str_val) {
    add_energy = parseInt(add_energy);
    var role_model = player_manager.role;
    role_model.addEnergy(add_energy);

    var role_id = player_manager.getRoleId();
    var channel_id = role_model.channel_id;
    analyze.recordEnergyGet(role_id, channel_id, module_type, operation_type, add_energy, reserve_int_val, reserve_str_val);
};

/**
 * 消耗体力
 */
exports.subEnergy = function (player_manager, module_type, operation_type, sub_energy, reserve_int_val, reserve_str_val) {
    sub_energy = parseInt(sub_energy);
    var role_model = player_manager.role;
    var copy_model = player_manager.copy;

    var success = role_model.subEnergy(sub_energy);
    if (!success) {
        return false;
    }
    // 黑市体力消耗统计
    copy_model.addTotalEnergy(sub_energy);

    // 是否触发黑市
    if (!!blackMarketConf.isTrigger(copy_model.total_energy)) {
        // 扣除黑市次数
        if (copy_model.is_end_time < moment().unix()) {
            if (!!copy_model.subBlackMarketNum()) {
                copy_model.setBlackMarketEndTime();
                // 重置列表和已购买列表
                copy_model.resBlackmarketList();
            }
        }
    }

    // 增加7天消耗统计
    var accrued_model = player_manager.accrued;
    accrued_model.addStaticList(accruedType.SUB_ENERGY, sub_energy);

    var role_id = player_manager.getRoleId();
    var channel_id = role_model.channel_id;
    analyze.recordEnergyUse(role_id, channel_id, module_type, operation_type, sub_energy, reserve_int_val, reserve_str_val);

    return success;
};

/**
 * 添加精力（无上限限制）
 * @param {[type]} add_gold [description]
 */
exports.addSmithyEnergy = function (player_manager, module_type, operation_type, add_smithy_energy, reserve_int_val, reserve_str_val) {
    add_smithy_energy = parseInt(+add_smithy_energy);
    var smithy_model = player_manager.smithy;
    var role_model = player_manager.role;

    smithy_model.addEnergy(add_smithy_energy);
    // taskService.addTotalSmithyEnergy(player_manager, add_smithy_energy);

    var role_id = player_manager.getRoleId();
    var channel_id = role_model.channel_id;
    analyze.recordSmithyEnergyGet(role_id, channel_id, module_type, operation_type, add_smithy_energy, reserve_int_val, reserve_str_val);

    return add_smithy_energy;
};

/**
 * 消耗精力
 * @param {[type]} add_gold [description]
 */
exports.subSmithyEnergy = function (player_manager, module_type, operation_type, sub_smithy_energy, reserve_int_val, reserve_str_val) {
    sub_smithy_energy = parseInt(+sub_smithy_energy);
    var smithy_model = player_manager.smithy;
    var role_model = player_manager.role;

    var success = smithy_model.subEnergy(sub_smithy_energy);
    // taskService.subTotalSmithyEnergy(player_manager, sub_smithy_energy);

    // 增加7天消耗统计
    var accrued_model = player_manager.accrued;
    accrued_model.addStaticList(accruedType.SUB_SMITHY_ENERGY, sub_smithy_energy);

    if (!!success) {
        var role_id = player_manager.getRoleId();
        var channel_id = role_model.channel_id;
        analyze.recordSmithyEnergyGet(role_id, channel_id, module_type, operation_type, sub_smithy_energy, reserve_int_val, reserve_str_val);
    }

    return success;
};

/**
 * 添加魔能
 * @param {[type]} add_magic [description]
 */
exports.addMagic = function (player_manager, add_magic) {
    add_magic = parseInt(add_magic);
    player_manager.role.addMagic(add_magic);
};

/**
 * 扣除魔能
 * @param  {[type]} player_manager [description]
 * @return {[type]}                [description]
 */
exports.subMagic = function (player_manager, sub_magic) {
    sub_magic = parseInt(sub_magic);
    return player_manager.role.subMagic(sub_magic);
};

/**
 * 同步角色等级(更新旧库统计数据)
 * @param {[type]} player_manager [description]
 * @param {[type]} add_exp        [description]
 */
exports.syncRoleLevel = function (cb) {
    var where = {
        lv: {
            $gt: 1
        }
    };

    roleModel.findRoles(where, function (err, role_list) {
        async.eachSeries(role_list, function (role, cb1) {
            var role_id = role.role_id;
            var role_lv = role.lv;
            analyze.recordRoleLevel(role_id, role_lv, cb1);
        }, function (err) {
            cb();
        });
    });
};
